﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MeTalker.SDK.Core;

namespace MeTalker.Aide.Forms
{
   internal partial class TalkerTip : Form
    {
        TalkerForm from;
        public TalkerTip()
        {
            InitializeComponent();
            pictureBox.Image =  new Bitmap("icons/iconx.gif");
            this.Load += new EventHandler(TalkerTip_Load);
            this.FormClosing += new FormClosingEventHandler(TalkerTip_FormClosing);
        }
        public TalkerTip(TalkerForm from)
            : this()
        {
            this.from = from;
            SetLocation();
        }

        void SetLocation()
        {
            if (!WindowControl.IsWindowVisible(from.helper.HandleWinForm))
            {
                this.Close();
                return;
            }

            WindowControl.RECT rect;
            from.helper.MainRect(out rect);
            bool isMax = WindowControl.IsZoomed(from.helper.HandleWinForm);
            if (isMax)
            {
                WindowControl.ShowWindowAsync(from.helper.HandleWinForm, WindowControl.SW_RESTORE);
            }
            if (WindowControl.IsIconic(from.helper.HandleWinForm))
            {
                if (Visible)
                {
                    Visible = false;
                }
                return;
            }
            else
            {
                if (!this.Visible)
                {
                    this.Visible = true;
                }
            }

            if (rect.Right == 0 && rect.Top == 0 && rect.Left == 0 && rect.Bottom == 0)
            {
                this.Close();
                return;
            }
            if (this.Width != 32)
                this.Width = 32;
            if (this.Height != 78)
                this.Height = 78;

            if (rect.Right != this.Location.X && rect.Top != this.Location.Y)
                this.Location = new Point(rect.Right, rect.Top + 1);
        }

        void TalkerTip_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (timer1 != null)
                timer1.Enabled = false;
            timer1 = null;
        }

        void TalkerTip_Load(object sender, EventArgs e)
        {
            SetLocation();
            timer1.Interval = 100;
            timer1.Enabled = true;
        }

        private void btnDisp_Click(object sender, EventArgs e)
        {
            from.SetWinState(TalkerState.Display);
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                SetLocation();
                if (WindowControl.GetForegroundWindow() == from.helper.HandleWinForm)
                {
                    if (!this.TopMost)
                    {
                        TopMost = true;
                        TopMost = false;
                        from.helper.SetForegroundMainWindow();
                    }
                }
                else {
                    if (this.TopMost)
                    {
                        TopMost = false;
                    }
                }
            }
            catch (Exception ex)
            {
                if (UtilHelper.DEBUG)
                    MuchInfo.Logs.MILog.Debug("timer1_Tick", ex);
            }
        }
    }
}